import sys
sys.path.insert(0, sys.path[0]+"/../")

import numpy as np
import math
from typing import List, Tuple, Optional
from .config import GameConfig
from .units import Unit, WhiteBoat, WhiteDrone, BlackBoat

class CollisionSystem:
    """
    碰撞检测系统
    """
    
    @staticmethod
    def check_all_collisions(white_boats: List[WhiteBoat], black_boats: List[BlackBoat]) -> int:
        """
        检查所有单位间的碰撞
        
        Args:
            white_boats (List[WhiteBoat]): 白方无人艇列表
            black_boats (List[BlackBoat]): 黑方无人艇列表
            
        Returns:
            int: 碰撞次数
        """
        collision_count = 0
        
        # 检查白方无人艇之间的碰撞
        collision_count += CollisionSystem._check_white_boat_collisions(white_boats)
        
        # 检查白方与黑方无人艇之间的碰撞
        collision_count += CollisionSystem._check_white_black_collisions(white_boats, black_boats)
        
        # 检查黑方无人艇之间的碰撞
        collision_count += CollisionSystem._check_black_boat_collisions(black_boats)
        
        
        return collision_count
    
    @staticmethod
    def count_white_involved_collisions(white_boats: List[WhiteBoat], black_boats: List[BlackBoat]) -> int:
        """
        统计当前时刻“涉及白方”的碰撞配对数（采样用）。
        包含：
        - 白方无人艇之间的碰撞配对数
        - 白方与黑方无人艇之间的碰撞配对数
        不包含：
        - 黑方无人艇之间的碰撞配对数
        """
        collision_count = 0
        # 白白配对
        collision_count += CollisionSystem._check_white_boat_collisions(white_boats)
        # 白黑配对
        collision_count += CollisionSystem._check_white_black_collisions(white_boats, black_boats)
        return collision_count
    
    @staticmethod
    def _check_white_boat_collisions(white_boats: List[WhiteBoat]) -> int:
        """检查白方无人艇之间的碰撞"""
        collision_count = 0
        for i, boat1 in enumerate(white_boats):
            if not boat1.active:
                continue
            for j, boat2 in enumerate(white_boats[i+1:], i+1):
                if not boat2.active:
                    continue
                distance = boat1.distance_to(boat2)
                if distance < GameConfig.COLLISION_DISTANCE:
                    collision_count += 1
        return collision_count
    
    @staticmethod
    def _check_white_black_collisions(white_boats: List[WhiteBoat], black_boats: List[BlackBoat]) -> int:
        """检查白方与黑方无人艇之间的碰撞"""
        collision_count = 0
        for white_boat in white_boats:
            if not white_boat.active:
                continue
            for black_boat in black_boats:
                if not black_boat.active:
                    continue
                distance = white_boat.distance_to(black_boat)
                if distance < GameConfig.COLLISION_DISTANCE:
                    collision_count += 1
        return collision_count
    
    @staticmethod
    def _check_black_boat_collisions(black_boats: List[BlackBoat]) -> int:
        """检查黑方无人艇之间的碰撞"""
        collision_count = 0
        for i, boat1 in enumerate(black_boats):
            if not boat1.active:
                continue
            for j, boat2 in enumerate(black_boats[i+1:], i+1):
                if not boat2.active:
                    continue
                distance = boat1.distance_to(boat2)
                if distance < GameConfig.COLLISION_DISTANCE:
                    collision_count += 1
        return collision_count
    
    # @staticmethod
    # def _check_drone_collisions(white_drones: List[WhiteDrone]) -> int:
    #     """检查无人机之间的碰撞""" #无人机间不用碰撞检测
    #     collision_count = 0
    #     for i, drone1 in enumerate(white_drones):
    #         if drone1.state != "flying":
    #             continue
    #         for j, drone2 in enumerate(white_drones[i+1:], i+1):
    #             if drone2.state != "flying":
    #                 continue
    #             distance = drone1.distance_to(drone2)
    #             if distance < GameConfig.COLLISION_DISTANCE:
    #                 collision_count += 1
    #     return collision_count
